home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / flex_247.zip / flex_247 / misc.c < prev    next >
C/C++ Source or Header  |  1994-01-04  |  14KB  |  774 lines

  1. /* misc - miscellaneous flex routines */
  2.  
  3. /*-
  4.  * Copyright (c) 1990 The Regents of the University of California.
  5.  * All rights reserved.
  6.  *
  7.  * This code is derived from software contributed to Berkeley by
  8.  * Vern Paxson.
  9.  * 
  10.  * The United States Government has rights in this work pursuant
  11.  * to contract no. DE-AC03-76SF00098 between the United States
  12.  * Department of Energy and the University of California.
  13.  *
  14.  * Redistribution and use in source and binary forms are permitted provided
  15.  * that: (1) source distributions retain this entire copyright notice and
  16.  * comment, and (2) distributions including binaries display the following
  17.  * acknowledgement:  ``This product includes software developed by the
  18.  * University of California, Berkeley and its contributors'' in the
  19.  * documentation or other materials provided with the distribution and in
  20.  * all advertising materials mentioning features or use of this software.
  21.  * Neither the name of the University nor the names of its contributors may
  22.  * be used to endorse or promote products derived from this software without
  23.  * specific prior written permission.
  24.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  25.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  26.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  27.  */
  28.  
  29. /* $Header: misc.c,v 1.2 94/01/04 14:33:10 vern Exp $ */
  30.  
  31. #include "flexdef.h"
  32.  
  33.  
  34.  
  35. /* declare functions that have forward references */
  36.  
  37. void dataflush PROTO((void));
  38. int otoi PROTO((Char []));
  39.  
  40.  
  41. void add_action( new_text )
  42. char *new_text;
  43.     {
  44.     int len = strlen( new_text );
  45.  
  46.     while ( len + action_index >= action_size - 10 /* slop */ )
  47.         {
  48.         action_size *= 2;
  49.         action_array =
  50.             reallocate_character_array( action_array, action_size );
  51.         }
  52.  
  53.     strcpy( &action_array[action_index], new_text );
  54.  
  55.     action_index += len;
  56.     }
  57.  
  58.  
  59. /* allocate_array - allocate memory for an integer array of the given size */
  60.  
  61. void *allocate_array( size, element_size )
  62. int size, element_size;
  63.     {
  64.     register void *mem;
  65.  
  66.     /* On 16-bit int machines (e.g., 80286) we might be trying to
  67.      * allocate more than a signed int can hold, and that won't
  68.      * work.  Cheap test:
  69.      */
  70.     if ( element_size * size <= 0 )
  71.         flexfatal( "request for < 1 byte in allocate_array()" );
  72.  
  73.     mem = flex_alloc( element_size * size );
  74.  
  75.     if ( mem == NULL )
  76.         flexfatal( "memory allocation failed in allocate_array()" );
  77.  
  78.     return mem;
  79.     }
  80.  
  81.  
  82. /* all_lower - true if a string is all lower-case */
  83.  
  84. int all_lower( str )
  85. register char *str;
  86.     {
  87.     while ( *str )
  88.         {
  89.         if ( ! isascii( (Char) *str ) || ! islower( *str ) )
  90.             return 0;
  91.         ++str;
  92.         }
  93.  
  94.     return 1;
  95.     }
  96.  
  97.  
  98. /* all_upper - true if a string is all upper-case */
  99.  
  100. int all_upper( str )
  101. register char *str;
  102.     {
  103.     while ( *str )
  104.         {
  105.         if ( ! isascii( (Char) *str ) || ! isupper( *str ) )
  106.             return 0;
  107.         ++str;
  108.         }
  109.  
  110.     return 1;
  111.     }
  112.  
  113.  
  114. /* bubble - bubble sort an integer array in increasing order
  115.  *
  116.  * synopsis
  117.  *   int v[n], n;
  118.  *   void bubble( v, n );
  119.  *
  120.  * description
  121.  *   sorts the first n elements of array v and replaces them in
  122.  *   increasing order.
  123.  *
  124.  * passed
  125.  *   v - the array to be sorted
  126.  *   n - the number of elements of 'v' to be sorted
  127.  */
  128.  
  129. void bubble( v, n )
  130. int v[], n;
  131.     {
  132.     register int i, j, k;
  133.  
  134.     for ( i = n; i > 1; --i )
  135.         for ( j = 1; j < i; ++j )
  136.             if ( v[j] > v[j + 1] )    /* compare */
  137.                 {
  138.                 k = v[j];    /* exchange */
  139.                 v[j] = v[j + 1];
  140.                 v[j + 1] = k;
  141.                 }
  142.     }
  143.  
  144.  
  145. /* check_char - checks a character to make sure it's within the range
  146.  *        we're expecting.  If not, generates fatal error message
  147.  *        and exits.
  148.  */
  149.  
  150. void check_char( c )
  151. int c;
  152.     {
  153.     if ( c >= CSIZE )
  154.         lerrsf( "bad character '%s' detected in check_char()",
  155.             readable_form( c ) );
  156.  
  157.     if ( c >= csize )
  158.         lerrsf( "scanner requires -8 flag to use the character '%s'",
  159.             readable_form( c ) );
  160.     }
  161.  
  162.  
  163.  
  164. /* clower - replace upper-case letter to lower-case */
  165.  
  166. Char clower( c )
  167. register int c;
  168.     {
  169.     return (Char) ((isascii( c ) && isupper( c )) ? tolower( c ) : c);
  170.     }
  171.  
  172.  
  173. /* copy_string - returns a dynamically allocated copy of a string */
  174.  
  175. char *copy_string( str )
  176. register char *str;
  177.     {
  178.     register char *c;
  179.     char *copy;
  180.  
  181.     /* find length */
  182.     for ( c = str; *c; ++c )
  183.         ;
  184.  
  185.     copy = (char *) flex_alloc( (c - str + 1) * sizeof( char ) );
  186.  
  187.     if ( copy == NULL )
  188.         flexfatal( "dynamic memory failure in copy_string()" );
  189.  
  190.     for ( c = copy; (*c++ = *str++); )
  191.         ;
  192.  
  193.     return copy;
  194.     }
  195.  
  196.  
  197. /* copy_unsigned_string -
  198.  *    returns a dynamically allocated copy of a (potentially) unsigned string
  199.  */
  200.  
  201. Char *copy_unsigned_string( str )
  202. register Char *str;
  203.     {
  204.     register Char *c;
  205.     Char *copy;
  206.  
  207.     /* find length */
  208.     for ( c = str; *c; ++c )
  209.         ;
  210.  
  211.     copy = allocate_Character_array( c - str + 1 );
  212.  
  213.     for ( c = copy; (*c++ = *str++); )
  214.         ;
  215.  
  216.     return copy;
  217.     }
  218.  
  219.  
  220. /* cshell - shell sort a character array in increasing order
  221.  *
  222.  * synopsis
  223.  *
  224.  *   Char v[n];
  225.  *   int n, special_case_0;
  226.  *   cshell( v, n, special_case_0 );
  227.  *
  228.  * description
  229.  *   Does a shell sort of the first n elements of array v.
  230.  *   If special_case_0 is true, then any element equal to 0
  231.  *   is instead assumed to have infinite weight.
  232.  *
  233.  * passed
  234.  *   v - array to be sorted
  235.  *   n - number of elements of v to be sorted
  236.  */
  237.  
  238. void cshell( v, n, special_case_0 )
  239. Char v[];
  240. int n, special_case_0;
  241.     {
  242.     int gap, i, j, jg;
  243.     Char k;
  244.  
  245.     for ( gap = n / 2; gap > 0; gap = gap / 2 )
  246.         for ( i = gap; i < n; ++i )
  247.             for ( j = i - gap; j >= 0; j = j - gap )
  248.                 {
  249.                 jg = j + gap;
  250.  
  251.                 if ( special_case_0 )
  252.                     {
  253.                     if ( v[jg] == 0 )
  254.                         break;
  255.  
  256.                     else if ( v[j] != 0 && v[j] <= v[jg] )
  257.                         break;
  258.                     }
  259.  
  260.                 else if ( v[j] <= v[jg] )
  261.                     break;
  262.  
  263.                 k = v[j];
  264.                 v[j] = v[jg];
  265.                 v[jg] = k;
  266.                 }
  267.     }
  268.  
  269.  
  270. /* dataend - finish up a block of data declarations */
  271.  
  272. void dataend()
  273.     {
  274.     if ( datapos > 0 )
  275.         dataflush();
  276.  
  277.     /* add terminator for initialization; { for vi */
  278.     puts( "    } ;\n" );
  279.  
  280.     dataline = 0;
  281.     datapos = 0;
  282.     }
  283.  
  284.  
  285. /* dataflush - flush generated data statements */
  286.  
  287. void dataflush()
  288.     {
  289.     putchar( '\n' );
  290.  
  291.     if ( ++dataline >= NUMDATALINES )
  292.         {
  293.         /* Put out a blank line so that the table is grouped into
  294.          * large blocks that enable the user to find elements easily.
  295.          */
  296.         putchar( '\n' );
  297.         dataline = 0;
  298.         }
  299.  
  300.     /* Reset the number of characters written on the current line. */
  301.     datapos = 0;
  302.     }
  303.  
  304.  
  305. /* flexerror - report an error message and terminate */
  306.  
  307. void flexerror( msg )
  308. char msg[];
  309.     {
  310.     fprintf( stderr, "%s: %s\n", program_name, msg );
  311.     flexend( 1 );
  312.     }
  313.  
  314.  
  315. /* flexfatal - report a fatal error message and terminate */
  316.  
  317. void flexfatal( msg )
  318. char msg[];
  319.     {
  320.     fprintf( stderr, "%s: fatal internal error, %s\n", program_name, msg );
  321.     exit( 1 );
  322.     }
  323.  
  324.  
  325. /* lerrif - report an error message formatted with one integer argument */
  326.  
  327. void lerrif( msg, arg )
  328. char msg[];
  329. int arg;
  330.     {
  331.     char errmsg[MAXLINE];
  332.     (void) sprintf( errmsg, msg, arg );
  333.     flexerror( errmsg );
  334.     }
  335.  
  336.  
  337. /* lerrsf - report an error message formatted with one string argument */
  338.  
  339. void lerrsf( msg, arg )
  340. char msg[], arg[];
  341.     {
  342.     char errmsg[MAXLINE];
  343.  
  344.     (void) sprintf( errmsg, msg, arg );
  345.     flexerror( errmsg );
  346.     }
  347.  
  348.  
  349. /* htoi - convert a hexadecimal digit string to an integer value */
  350.  
  351. int htoi( str )
  352. Char str[];
  353.     {
  354.     unsigned int result;
  355.  
  356.     (void) sscanf( (char *) str, "%x", &result );
  357.  
  358.     return result;
  359.     }
  360.  
  361.  
  362. /* is_hex_digit - returns true if a character is a valid hex digit, false
  363.  *          otherwise
  364.  */
  365.  
  366. int is_hex_digit( ch )
  367. int ch;
  368.     {
  369.     if ( isdigit( ch ) )
  370.         return 1;
  371.  
  372.     switch ( clower( ch ) )
  373.         {
  374.         case 'a':
  375.         case 'b':
  376.         case 'c':
  377.         case 'd':
  378.         case 'e':
  379.         case 'f':
  380.             return 1;
  381.  
  382.         default:
  383.             return 0;
  384.         }
  385.     }
  386.  
  387.  
  388. /* line_directive_out - spit out a "# line" statement */
  389.  
  390. void line_directive_out( output_file )
  391. FILE *output_file;
  392.     {
  393.     if ( infilename && gen_line_dirs )
  394.         {
  395.         char directive[MAXLINE];
  396.         sprintf( directive, "# line %d \"%s\"\n", linenum, infilename );
  397.  
  398.         /* If output_file is nil then we should put the directive in
  399.          * the accumulated actions.
  400.          */
  401.         if ( output_file )
  402.             fputs( directive, output_file );
  403.         else
  404.             add_action( directive );
  405.         }
  406.     }
  407.  
  408.  
  409. /* mark_defs1 - mark the current position in the action array as
  410.  *               representing where the user's section 1 definitions end
  411.  *         and the prolog begins
  412.  */
  413. void mark_defs1()
  414.     {
  415.     defs1_offset = 0;
  416.     action_array[action_index++] = '\0';
  417.     action_offset = prolog_offset = action_index;
  418.     action_array[action_index] = '\0';
  419.     }
  420.  
  421.  
  422. /* mark_prolog - mark the current position in the action array as
  423.  *               representing the end of the action prolog
  424.  */
  425. void mark_prolog()
  426.     {
  427.     action_array[action_index++] = '\0';
  428.     action_offset = action_index;
  429.     action_array[action_index] = '\0';
  430.     }
  431.  
  432.  
  433. /* mk2data - generate a data statement for a two-dimensional array
  434.  *
  435.  * Generates a data statement initializing the current 2-D array to "value".
  436.  */
  437. void mk2data( value )
  438. int value;
  439.     {
  440.     if ( datapos >= NUMDATAITEMS )
  441.         {
  442.         putchar( ',' );
  443.         dataflush();
  444.         }
  445.  
  446.     if ( datapos == 0 )
  447.         /* Indent. */
  448.         fputs( "    ", stdout );
  449.  
  450.     else
  451.         putchar( ',' );
  452.  
  453.     ++datapos;
  454.  
  455.     printf( "%5d", value );
  456.     }
  457.  
  458.  
  459. /* mkdata - generate a data statement
  460.  *
  461.  * Generates a data statement initializing the current array element to
  462.  * "value".
  463.  */
  464. void mkdata( value )
  465. int value;
  466.     {
  467.     if ( datapos >= NUMDATAITEMS )
  468.         {
  469.         putchar( ',' );
  470.         dataflush();
  471.         }
  472.  
  473.     if ( datapos == 0 )
  474.         /* Indent. */
  475.         fputs( "    ", stdout );
  476.     else
  477.         putchar( ',' );
  478.  
  479.     ++datapos;
  480.  
  481.     printf( "%5d", value );
  482.     }
  483.  
  484.  
  485. /* myctoi - return the integer represented by a string of digits */
  486.  
  487. int myctoi( array )
  488. char array[];
  489.     {
  490.     int val = 0;
  491.  
  492.     (void) sscanf( array, "%d", &val );
  493.  
  494.     return val;
  495.     }
  496.  
  497.  
  498. /* myesc - return character corresponding to escape sequence */
  499.  
  500. Char myesc( array )
  501. Char array[];
  502.     {
  503.     Char c, esc_char;
  504.  
  505.     switch ( array[1] )
  506.         {
  507.         case 'b': return '\b';
  508.         case 'f': return '\f';
  509.         case 'n': return '\n';
  510.         case 'r': return '\r';
  511.         case 't': return '\t';
  512.  
  513. #ifdef __STDC__
  514.         case 'a': return '\a';
  515.         case 'v': return '\v';
  516. #else
  517.         case 'a': return '\007';
  518.         case 'v': return '\013';
  519. #endif
  520.  
  521.         case '0':
  522.         case '1':
  523.         case '2':
  524.         case '3':
  525.         case '4':
  526.         case '5':
  527.         case '6':
  528.         case '7':
  529.         case '8':
  530.         case '9':
  531.             { /* \<octal> */
  532.             int sptr = 1;
  533.  
  534.             while ( isascii( array[sptr] ) &&
  535.                 isdigit( array[sptr] ) )
  536.                 /* Don't increment inside loop control
  537.                  * because if isdigit() is a macro it might
  538.                  * expand into multiple increments ...
  539.                  */
  540.                 ++sptr;
  541.  
  542.             c = array[sptr];
  543.             array[sptr] = '\0';
  544.  
  545.             esc_char = otoi( array + 1 );
  546.  
  547.             array[sptr] = c;
  548.  
  549.             return esc_char;
  550.             }
  551.  
  552.         case 'x':
  553.             { /* \x<hex> */
  554.             int sptr = 2;
  555.  
  556.             while ( isascii( array[sptr] ) &&
  557.                 is_hex_digit( (char) array[sptr] ) )
  558.                 /* Don't increment inside loop control
  559.                  * because if isdigit() is a macro it might
  560.                  * expand into multiple increments ...
  561.                  */
  562.                 ++sptr;
  563.  
  564.             c = array[sptr];
  565.             array[sptr] = '\0';
  566.  
  567.             esc_char = htoi( array + 2 );
  568.  
  569.             array[sptr] = c;
  570.  
  571.             return esc_char;
  572.             }
  573.  
  574.         default:
  575.             return array[1];
  576.         }
  577.     }
  578.  
  579.  
  580. /* otoi - convert an octal digit string to an integer value */
  581.  
  582. int otoi( str )
  583. Char str[];
  584.     {
  585.     unsigned int result;
  586.  
  587.     (void) sscanf( (char *) str, "%o", &result );
  588.     return result;
  589.     }
  590.  
  591.  
  592. /* readable_form - return the the human-readable form of a character
  593.  *
  594.  * The returned string is in static storage.
  595.  */
  596.  
  597. char *readable_form( c )
  598. register int c;
  599.     {
  600.     static char rform[10];
  601.  
  602.     if ( (c >= 0 && c < 32) || c >= 127 )
  603.         {
  604.         switch ( c )
  605.             {
  606.             case '\b': return "\\b";
  607.             case '\f': return "\\f";
  608.             case '\n': return "\\n";
  609.             case '\r': return "\\r";
  610.             case '\t': return "\\t";
  611.  
  612. #ifdef __STDC__
  613.             case '\a': return "\\a";
  614.             case '\v': return "\\v";
  615. #endif
  616.  
  617.             default:
  618.                 (void) sprintf( rform, "\\%.3o",
  619.                         (unsigned int) c );
  620.                 return rform;
  621.             }
  622.         }
  623.  
  624.     else if ( c == ' ' )
  625.         return "' '";
  626.  
  627.     else
  628.         {
  629.         rform[0] = c;
  630.         rform[1] = '\0';
  631.  
  632.         return rform;
  633.         }
  634.     }
  635.  
  636.  
  637. /* reallocate_array - increase the size of a dynamic array */
  638.  
  639. void *reallocate_array( array, size, element_size )
  640. void *array;
  641. int size, element_size;
  642.     {
  643.     register void *new_array;
  644.  
  645.     /* Same worry as in allocate_array(): */
  646.     if ( size * element_size <= 0 )
  647.         flexfatal(
  648.             "attempt to increase array size by less than 1 byte" );
  649.  
  650.     new_array = flex_realloc( array, size * element_size );
  651.  
  652.     if ( new_array == NULL )
  653.         flexfatal( "attempt to increase array size failed" );
  654.  
  655.     return new_array;
  656.     }
  657.  
  658.  
  659. /* skelout - write out one section of the skeleton file
  660.  *
  661.  * Description
  662.  *    Copies skelfile or skel array to stdout until a line beginning with
  663.  *    "%%" or EOF is found.
  664.  */
  665. void skelout()
  666.     {
  667.     char buf_storage[MAXLINE];
  668.     char *buf = buf_storage;
  669.     int do_copy = 1;
  670.  
  671.     /* Loop pulling lines either from the skelfile, if we're using
  672.      * one, or from the skel[] array.
  673.      */
  674.     while ( skelfile ?
  675.         (fgets( buf, MAXLINE, skelfile ) != NULL) :
  676.         ((buf = skel[skel_ind++]) != 0) )
  677.         { /* copy from skel array */
  678.         if ( buf[0] == '%' )
  679.             { /* control line */
  680.             switch ( buf[1] )
  681.                 {
  682.                 case '%':
  683.                     return;
  684.  
  685.                 case '+':
  686.                     do_copy = C_plus_plus;
  687.                     break;
  688.  
  689.                 case '-':
  690.                     do_copy = ! C_plus_plus;
  691.                     break;
  692.  
  693.                 case '*':
  694.                     do_copy = 1;
  695.                     break;
  696.  
  697.                 default:
  698.                     flexfatal(
  699.                         "bad line in skeleton file" );
  700.                 }
  701.             }
  702.  
  703.         else if ( do_copy )
  704.             {
  705.             if ( skelfile )
  706.                 /* Skeleton file reads include final
  707.                  * newline, skel[] array does not.
  708.                  */
  709.                 fputs( buf, stdout );
  710.             else
  711.                 printf( "%s\n", buf );
  712.             }
  713.         }
  714.     }
  715.  
  716.  
  717. /* transition_struct_out - output a yy_trans_info structure
  718.  *
  719.  * outputs the yy_trans_info structure with the two elements, element_v and
  720.  * element_n.  Formats the output with spaces and carriage returns.
  721.  */
  722.  
  723. void transition_struct_out( element_v, element_n )
  724. int element_v, element_n;
  725.     {
  726.     printf( "%7d, %5d,", element_v, element_n );
  727.  
  728.     datapos += TRANS_STRUCT_PRINT_LENGTH;
  729.  
  730.     if ( datapos >= 75 )
  731.         {
  732.         putchar( '\n' );
  733.  
  734.         if ( ++dataline % 10 == 0 )
  735.             putchar( '\n' );
  736.  
  737.         datapos = 0;
  738.         }
  739.     }
  740.  
  741.  
  742. /* The following is only needed when building flex's parser using certain
  743.  * broken versions of bison.
  744.  */
  745. void *yy_flex_xmalloc( size )
  746. int size;
  747.     {
  748.     void *result = flex_alloc( size );
  749.  
  750.     if ( ! result  )
  751.         flexfatal( "memory allocation failed in yy_flex_xmalloc()" );
  752.  
  753.     return result;
  754.     }
  755.  
  756.  
  757. /* zero_out - set a region of memory to 0
  758.  *
  759.  * Sets region_ptr[0] through region_ptr[size_in_bytes - 1] to zero.
  760.  */
  761.  
  762. void zero_out( region_ptr, size_in_bytes )
  763. char *region_ptr;
  764. int size_in_bytes;
  765.     {
  766.     register char *rp, *rp_end;
  767.  
  768.     rp = region_ptr;
  769.     rp_end = region_ptr + size_in_bytes;
  770.  
  771.     while ( rp < rp_end )
  772.         *rp++ = 0;
  773.     }
  774.